<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<form id="menu-edit-form" method="post" style="pediting: 10px;">  
	<input name="id" type="hidden" value="${menu.id}" /> 
    <div>   
        <label for="name">名称:</label><span>&nbsp;&nbsp;&nbsp;&nbsp;</span>   
        <input class="easyui-validatebox" type="text" name="name" value="${menu.name }"
         data-options="required:true" onblur="menu.checkEditName(this.value)" />
        <span id="name-tip"></span>
    </div>
    <br />   
    <div>   
        <label for="url">url:</label><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>   
        <input type="text" name="url" value="${menu.url }"/>
    </div>    
</form>  

<script type="text/javascript">
	menu.edit=function(){
		$('#menu-edit-form').form('submit', {
			url: '/menu/edit',
			onSubmit: function(){
				var isValid = $(this).form('validate');
				var name = $('#menu-edit-form input[name="name"]').val();
				var flag = menu.checkEditName(name);
				return isValid && flag;	// 返回false终止表单提交
			},
			success: function(data){
				//把字符串转化成 json对象
				var data = JSON.parse(data);
				if(data.code == 200){
					//后端修改成功后，再修改前端
					var node = $('#menu-list-tree').tree('getSelected');
					if (node){
						$('#menu-list-tree').tree('update', {
							target: node.target,
							text: $('#menu-edit-form input[name="name"]').val()
						});
					}
					$.messager.alert('提示','修改成功！','info');
				}
			}
		});
	}
	
	menu.checkEditName=function(name){
		var orignal_name = '${menu.name}';
		var name = $.trim(name);
		if(name == ''){
			return;
		}
		if(orignal_name == name){
			return true;
		}
		//名称被占用
		var flag= false;
		
		//改成同步
		$.ajax({
		   type: "get",
		   async:false,
		   url: "/menu/name/check",
		   data: "name="+name,
		   success: function(msg){
		     if(msg.code == 200 && !msg.data){
		    	 $('#name-tip').html("<font color='green'>该名称可以使用</font>");
		    	 flag = true;
		     }else if(msg.code == 200 && msg.data){
		    	 $('#name-tip').html("<font color='red'>该名称已被使用</font>");
		     }
		   }
		});
		
		return flag;
	}
</script>